# Adogs
# 时间: 2021/8/13 11:13
import pygame


class Mine(pygame.sprite.Sprite):
    """
    设置地雷
    方法：
    1.__init__(x, y, image)
    2.draw(current_surface, view_x, view_y)
    """
    def __init__(self, x, y, image):
        """
        初始化函数
        :param x: 地雷的x坐标
        :param y: 地雷的y坐标
        :param image: 地雷的图片路径
        """
        super(Mine, self).__init__()
        self.pos_x = x
        self.pos_y = y
        self.image = image
        (x, y, w, h) = self.image.get_rect()
        self.rect = pygame.Rect(self.pos_x, self.pos_y, w, h)

    def draw(self, current_surface, view_x, view_y):
        """
        地雷绘制函数
        :param current_surface: 当前的绘制屏幕
        :param view_x: 屏幕显示的x坐标
        :param view_y: 屏幕显示的y坐标
        :return:无返回值
        """
        current_surface.blit(self.image, (self.pos_x - view_x, self.pos_y - view_y))


class Boom(pygame.sprite.Sprite):
    """
    地雷爆炸
    方法：
    1.__init__(x, y, image, row, col)
    2.draw(surface, view_x, view_y)
    3.kill_flag()
    """
    def __init__(self, x, y, image, row, col):
        """
        初始化函数
        :param x: 地雷的x坐标
        :param y: 地雷的y坐标
        :param image: 图片路径
        :param row: 图片的行数
        :param col: 图片的列数
        """
        super(Boom, self).__init__()
        self.row = row
        self.col = col
        self.pos_x = x
        self.pos_y = y
        self.image = image
        (x, y, w, h) = self.image.get_rect()
        self.rect = pygame.Rect(self.pos_x, self.pos_y, w, h)
        w = int(w / col)
        h = int(h / row)    # 求出单个图片的宽高
        self.no = 0     # 图片显示信息
        self.count = row * col      # 图片的数量
        self.boom_image = []    # 存储爆炸图片
        self.show_count = 0  # 图片显示时间

        for i in range(0, self.count):  # 分割图片
            temp_row = int(i / self.col)
            temp_col = i % self.col
            rect = (temp_col * w, temp_row * h, w, h)
            image = self.image.subsurface(rect)
            self.boom_image.append(image)

    def draw(self, surface, view_x, view_y):
        """
        绘制函数
        :param surface:当前的绘制屏幕
        :param view_x: 屏幕的显示x坐标
        :param view_y: 屏幕的显示y坐标
        :return: 无返回值
        """
        if self.no >= self.count - 1:
            return

        self.show_count += 1

        if self.show_count > 1:     # 控制一帧爆炸图片需要显示多久
            self.no += 1
            self.show_count = 0
            if self.no >= self.count:
                return

        surface.blit(self.boom_image[self.no], (self.pos_x - view_x, self.pos_y - view_y))

    def kill_flag(self):
        """
        判断爆炸动画是否播放完毕
        :return:  True or False
        """
        if self.no >= self.count:
            return True
        else:
            return False
